layui.use('layer', function(){
    var layer = layui.layer;
    $(".invite_register").click(function(){
	    $(".register").show();
	    $(".register_errow").hide();
	})
	$(".pack_text2").click(function(){
		$(".register").show();
	})
	
	$(".register h1").click(function(){
		$(".register").hide();
        $(".register_errow").hide();
	})
    $('.register_go').click(function() {
        var token=$('#token').val();
        var register_user = $(".register_user").val();
        var patrn1 = /^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){2,20}$/;
        if(!patrn1.test(register_user)) {
            $(".register_errow").text("帐号格式错误，请重新输入").show();
				return false;
        }else{
           $(".register_errow").hide();
        }
        var register_pass = $(".register_pass").val();
        if(!patrn1.test(register_pass)) {
           		$(".register_errow").text("密码格式错误，请重新输入").show();
				return false
        }else{
           	$(".register_errow").hide();
        }
      
        var register_phone = $(".register_phone").val();
        var patrn2 = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if(!patrn2.test(register_phone)) {
            $(".register_errow").text("请输入正确的手机号").show();
				return false;
        }else{
            $(".register_errow").hide();
        }
        var regs = /^.{4}$/
        var verification_numb = $(".verification_numb").val();
        if(!regs.test(verification_numb)) {
           $(".register_errow").text("请输入正确的图形验证码").show();
				return false
        }else{
            $(".register_errow").hide();
        }
        var reg = /^\d{4}$/;
        var code = $(".code").val();
       if(!reg.test(code)) {
				$(".register_errow").text("短信验证码输入错误或过期").show();
				return false
			} else {
				$(".register_errow").hide();
		}
        var data = {
				'username': register_user,
				'password': register_pass,
				'mobile': register_phone,
				'validateCode': verification_numb,
				'code': code,
				'_token': token
			}


        // url = $('#url').val();
        $.ajax({
            url: '/agent/register',
            type: "POST",
            data: data,
            beforeSend:function(){
                $(".go_btn").attr("disabled",true);
            },
            success: function(ret){
                if(ret.error_code != 0)
                {
                    layui.layer.msg(ret.error_message, {
                        icon: 6,
                        time: 2000
                    });
                    $(".go_btn").attr("disabled",false);
                }else{
                    layer.open({
                        type: 1,
                        title: '恭喜您完成注册',
                        skin: 'layui-layer-demo',
                        btn: ['确定'],
                        anim: 2,
                        shadeClose: true,
                        content: '<p class="pp1">点击确定两秒自动跳转至 > 代理商后台系统</p>',
                        yes: function(index){
                            layer.close(index);
                            $(".even_stop").show();
                            window.setTimeout("window.location.href='http://admin.niumoip.com'",2000);
                        },
                        cancel: function(){
                        }
                    });
                    //
                    // self.location.href='/personal/center';
                }
            }
        });




    });
//倒计时
   //倒计时
		var wait = 60;
		var isclick = true;

		function time(o) {
			wait--;
			o.html(wait + '秒后获取验证码');
			if(wait == 0) {
				isclick = true;
				o.html('重新发送验证码');
				wait = 60;
				return false;
			}
			isclick = false;
			setTimeout(function() {
					time(o);
				},
				1000)
		}

    $('#getCode').click(function (){
        var token=$('#token').val();
        var register_user = $(".register_user").val();
        var _this=$(this);
        var register_pass = $(".register_pass").val();
        var register_phone = $(".register_phone").val();
        var verification_numb = $(".verification_numb").val();
        var patrn2 = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
       if(register_user != '' && register_pass != '' && register_pass != '' && verification_numb != '') {} else {
				layer.msg("内容不能为空");
				return false;
		}
       if(isclick) {
				if(!patrn2.test(register_phone)) {
					$(".register_errow").text("请输入正确的手机号").show();
					return false;
				} else {
					$(".register_errow").hide();
				}
				var data = {
					'validateCode': verification_numb,
					'mobile': register_phone,
					'action': 'register',
					'_token': token
				};

               $.ajax({
                   url: '/send/code',
                   type: "post",
                   data: data,
                   success: function(ret) {
                       if(ret.code != 0) {
                           layui.layer.msg(ret.msg);
                       } else {
                           time(_this);
                       }
                   }
               });



        }
    })


});